package com.pureut.quality.service.impl;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pureut.common.core.exception.GlobalException;
import com.pureut.common.core.utils.DateUtils;
import com.pureut.common.core.utils.StringUtils;
import com.pureut.quality.domain.ExternalAuditPlan;
import com.pureut.quality.domain.ExternalSuggestedReport;
import com.pureut.quality.domain.dto.ExternalSuggestedReportDto;
import com.pureut.quality.domain.vo.ExternalSuggestedReportVo;
import com.pureut.quality.export.ExternalAuditPlanExport;
import com.pureut.quality.export.ExternalSuggestedReportExport;
import com.pureut.quality.mapper.ExternalAuditPlanMapper;
import com.pureut.quality.mapper.ExternalSuggestedReportMapper;
import com.pureut.quality.service.ExternalAuditPlanService;
import com.pureut.quality.service.IExternalSuggestedReportService;

import com.pureut.system.api.FeignService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;


/**
 * 外审建议项报告Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-04-09
 */
@Service
public class ExternalSuggestedReportServiceImpl extends ServiceImpl<ExternalSuggestedReportMapper, ExternalSuggestedReport> implements IExternalSuggestedReportService
{
    @Resource
    private ExternalSuggestedReportMapper externalSuggestedReportMapper;

    @Resource
    FeignService feignService;


    /**
     * 查询外审建议项报告
     * 
     * @param id 外审建议项报告主键
     * @return 外审建议项报告
     */
    @Override
    public ExternalSuggestedReportDto selectExternalSuggestedReportById(Long id)
    {
        return externalSuggestedReportMapper.selectExternalSuggestedReportById(id);
    }

    /**
     * 查询外审建议项报告列表
     * 
     * @param externalSuggestedReportVo 外审建议项报告
     * @return 外审建议项报告
     */
    @Override
    public List<ExternalSuggestedReportDto> selectExternalSuggestedReportList(ExternalSuggestedReportVo externalSuggestedReportVo)
    {
        return externalSuggestedReportMapper.selectExternalSuggestedReportList(externalSuggestedReportVo);
    }

    /**
     * 新增外审建议项报告
     * 
     * @param externalSuggestedReportVo 外审建议项报告
     * @return 结果
     */
    @Override
    public boolean insertExternalSuggestedReport(ExternalSuggestedReportVo externalSuggestedReportVo)
    {
        String authorityCoding = feignService.getAuthorityCoding("quality:externalSuggestedReport:list");
        if (authorityCoding == null) {
            throw new GlobalException("该单据未进行配置，无法生成单据编码");
        }
        ExternalSuggestedReport externalSuggestedReport = new ExternalSuggestedReport();
        BeanUtils.copyProperties(externalSuggestedReportVo, externalSuggestedReport);
        externalSuggestedReport.setRecordNumber(authorityCoding);
        return save(externalSuggestedReport);
    }

    /**
     * 修改外审建议项报告
     * 
     * @param externalSuggestedReportVo 外审建议项报告
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateExternalSuggestedReport(ExternalSuggestedReportVo externalSuggestedReportVo)
    {
        ExternalSuggestedReport externalSuggestedReport = new ExternalSuggestedReport();
        BeanUtils.copyProperties(externalSuggestedReportVo, externalSuggestedReport);
        externalSuggestedReport.setId(externalSuggestedReportVo.getId());
        return updateById(externalSuggestedReport);
    }

    /**
     * 批量删除外审建议项报告
     * 
     * @param ids 需要删除的外审建议项报告主键
     * @return 结果
     */
    @Override
    public boolean deleteExternalSuggestedReportByIds(String ids)
    {
        return removeById(ids);
    }

    /**
     * 导出
     * @param externalSuggestedReportVo
     * @return
     */
    @Override
    public List<ExternalSuggestedReportExport> selectSuggestedReportExport(ExternalSuggestedReportVo externalSuggestedReportVo) {
        if (externalSuggestedReportVo.getIdStr() != null){
            String idStr = externalSuggestedReportVo.getIdStr();
            String[] split = idStr.split(",");
            externalSuggestedReportVo.setIds(Arrays.asList(split));
        }

        return externalSuggestedReportMapper.selectSuggestedExport(externalSuggestedReportVo);
    }

    /**
     * 导入
     * @param coCustomer
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String importSuggestedReport(List<ExternalSuggestedReportExport> coCustomer) {
        if (StringUtils.isNull(coCustomer) || coCustomer.size() == 0) {
            throw new GlobalException("导入外审建议项报告不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();

        for (ExternalSuggestedReportExport mouldModelExport : coCustomer) {
            ExternalSuggestedReport externalSuggestedReport = new ExternalSuggestedReport();
            try {
                String authorityCoding = feignService.getAuthorityCoding("quality:externalSuggestedReport:list");
                if (authorityCoding == null) {
                    throw new GlobalException("该单据未进行配置，无法生成单据编码");
                }
                // 导入审核日期
                if (mouldModelExport.getFillTime() != null){
                    Date date = DateUtils.parseDate(mouldModelExport.getFillTime());
                    externalSuggestedReport.setFillTime(date);
                }

                // 导入审核时间
                if (mouldModelExport.getPlannedCompletionTime() != null){
                    Date date = DateUtils.parseDate(mouldModelExport.getPlannedCompletionTime());
                    externalSuggestedReport.setPlannedCompletionTime(date);
                }

                // 导入审核时间
                if (mouldModelExport.getActualCompletionTime() != null){
                    Date date = DateUtils.parseDate(mouldModelExport.getActualCompletionTime());
                    externalSuggestedReport.setActualCompletionTime(date);
                }

                BeanUtils.copyProperties(mouldModelExport, externalSuggestedReport);
                externalSuggestedReport.setRecordNumber(authorityCoding);
                save(externalSuggestedReport);
                successNum++;
                successMsg.append(successNum).append("、审核部门").append(mouldModelExport.getAuditDepartment()).append(" 导入成功");

            } catch (Exception e) {
                failureNum++;
                String msg = failureNum + "、审核部门 " + mouldModelExport.getAuditDepartment() + " 导入失败：";
                failureMsg.append(msg).append(e.getMessage());
            }
        }
        if (failureNum > 0) {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new GlobalException(failureMsg.toString());
        } else {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }

        return successMsg.toString();
    }

}
